package client_v1;

import client.Client;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * @file Client_V1.java
 *
 * @version 0.3
 * @author James Dibble 10009689
 * 
 * Latest Source:
 * http://code.google.com/p/csp-server/source/browse/trunk/
 *                                           Server/src/client_v1/Client_V1.java
 */
public class Client_V1 {

    static Client client;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int intPort = -1;
        String strServer = "";
        boolean bolUsedArgsFailed = false;
        Scanner scnrInput = new Scanner(System.in);
        String strServerIP, strServerPort;

        System.out.println("Welcome to File Client V1");
        
        //Check if CMD Args have been used.
        if (args.length > 0) {
            try {
                strServer = args[0];
                intPort = Integer.parseInt(args[1]);
            } catch (Exception ex) {
                System.out.println("Usage <Server IP> <Port>");
                bolUsedArgsFailed = true;
            }
        }
        
        //If the user hasn't used CMD args get them to input them now.
        if (!bolUsedArgsFailed && intPort == -1) {
            System.out.print("\nCommand line args can also be used.\n"
                            + "Usage <Server IP> <Port>\n\n"
                            + "IP Address of Server:"
                            );
            strServerIP = scnrInput.nextLine();
            System.out.print("Server Port: ");
            strServerPort = scnrInput.nextLine();
            
            //Check the user has entered a valid IP address.
            if (strServerIP.matches(
                           "\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b")) {
                //Check the user has entered a valid port number.
                if (strServerPort.matches("\\b\\d{1,5}\\b")) {
                    intPort = Integer.parseInt(strServerPort);
                    
                    //Try to connect to the server with the specified parms.
                    try {
                        client = new Client(strServerIP, 
                                           intPort, 
                                           Client.Client_Type.DESCRIPTION_CLIENT
                                           );
                    } catch (UnknownHostException ex) {
                        ex.printStackTrace(System.out);
                        System.out.println("Server could not be found at this "
                                           + "IP and Port.  Closing.."
                                           );
                    } catch (IOException ex) {
                        ex.printStackTrace(System.out);
                        System.out.println("General Socket Error.  Closing..");
                    }
                } else {
                    System.out.println(String.format(
                                          "%s is not a valid Port.  Closing...",
                                          strServerPort)
                                       );
                }
            } else {
                System.out.println(String.format(
                                    "%s is not an IP Address.  Closing...",
                                    strServerIP)
                                   );
            }
        }
    }
}